package per.dg.keepalive.onepixel

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter

class ScreenBroadcastListener {

    var mContext : Context

    private var mScreenReceiver : ScreenBroadcastReceiver

    lateinit var mListener : ScreenStateListener

    constructor(context : Context ) {
        mContext = context.getApplicationContext();
        mScreenReceiver = ScreenBroadcastReceiver();
    }

    interface ScreenStateListener {

        fun onScreenOn();

        fun onScreenOff();
    }

    /**
     * screen状态广播接收者
     */
    private inner class ScreenBroadcastReceiver : BroadcastReceiver() {
        lateinit var action : String

        override fun onReceive(context : Context, intent : Intent) {
            action = intent.getAction().toString();
            if (Intent.ACTION_SCREEN_ON.equals(action)) { // 开屏
                mListener.onScreenOn();
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) { // 锁屏
                mListener.onScreenOff();
            }
        }
    }

    fun registerListener(listener : ScreenStateListener ) {
        mListener = listener;
        registerListener()
    }

    private fun registerListener() {
        var filter : IntentFilter = IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        mContext.registerReceiver(mScreenReceiver, filter);
    }

}